#include "pch.h"
#include "NormalMappedMaterial.h"
#include "Rendering/DeferredConstantBuffers.h"

using namespace DirectX;

NormalMappedMaterial::NormalMappedMaterial(
	DirectX::XMFLOAT4 meshColor,
	DirectX::XMFLOAT4 diffuseColor,
	DirectX::XMFLOAT4 specularColor,
	float specularExponent,
	float meshColorBlendPercent,
	float specularColorBlendPercent,
	_In_ ID3D11ShaderResourceView* textureResourceView,
	_In_ ID3D11ShaderResourceView* normalMapResourceView,
	_In_ ID3D11VertexShader* vertexShader,
	_In_ ID3D11PixelShader* pixelShader
	) :
	Material(meshColor, diffuseColor, specularColor, specularExponent, meshColorBlendPercent, specularColorBlendPercent, textureResourceView, vertexShader, pixelShader),
	m_normalMapRV(normalMapResourceView)
{
}

void NormalMappedMaterial::RenderSetup(
	_In_ ID3D11DeviceContext* context,
	_In_ DirectX::XMFLOAT4X4& world,
	_In_ ID3D11Buffer* primitiveConstantBuffer,
	_In_ bool isFirstPass
	)
{
	if (isFirstPass)
	{
		LightPrePassPerMesh constantBuffer = {};
		constantBuffer.World = world;
		constantBuffer.SpecularPower = m_specularExponent;

		context->UpdateSubresource(primitiveConstantBuffer, 0, nullptr, &constantBuffer, 0, 0);
		context->PSSetShaderResources(0, 1, m_normalMapRV.GetAddressOf());
		context->VSSetShader(m_vertexShader.Get(), nullptr, 0);
		context->PSSetShader(m_pixelShader.Get(), nullptr, 0);
	}
	else
	{
		LightFinalPassPerMesh constantBuffer = {};
		constantBuffer.DiffuseTint = m_diffuseColor;
		constantBuffer.SpecularColor = m_specularColor;
		constantBuffer.MeshColor = m_meshColor;
		constantBuffer.World = world;
		constantBuffer.OverridePercentDiffusePercent = m_meshPercentDiffusePercent;

		context->UpdateSubresource(primitiveConstantBuffer, 0, nullptr, &constantBuffer, 0, 0);
		context->PSSetShaderResources(1, 1, m_textureRV.GetAddressOf());
	}
}